<? 
	require_once('lib/nusoap.php'); 
	include("connect/connect.php");

	$ws_client = new soapclient('http://127.0.0.1/dsccmis/datasource_webservice/get_opmhelp.php?wsdl',true); 

	$result = $ws_client->call('getOpmhelp'); 

	if($result<>""){
		$last_rev_status="สำเร็จ";
	}else{
		$last_rev_status="ล้มเหลว";
	}

	$arr_rec = explode(";",$result); 

	//last_rev_record
	$last_rev_record=count($arr_rec)-1;
	
	//charset
	mysql_select_db("dscc",$c);
	mysql_query("set names utf8");
	
	$tempY=date("Y");
	if($tempY<"2550"){
		$tempY=(int)$tempY+543;
	}
	$datetime_receive= $tempY.date("-m-d-H:i");

	foreach($arr_rec as $rec){
		$fld=explode(",",$rec);
		if($fld[0]<>""){ // 
			$sql = "insert into rev_opmhelp(recid, pid, cause, rev_first_date, rev_first_amount, per_rev_first, rev_all_date, rev_all_amount, per_rev_all,datetime_receive) values('$fld[0]','$fld[1]','$fld[2]','$fld[3]','$fld[4]','$fld[5]','$fld[6]','$fld[7]','$fld[8]','$datetime_receive')";
			//echo "<br>".$sql;
			$rs=mysql_query($sql);
		}
	}

	//$total_record
	$sql = "select count(*) as c from rev_opmhelp";
	$rs=mysql_query($sql);
	$rows=mysql_fetch_array($rs);
	$total_record=$rows["c"];

	$sql = "update rev_status set total_record=$total_record,last_rev_record=$last_rev_record,last_rev_datetime='$datetime_receive',last_rev_status='$last_rev_status' where dsid='G2' and datasettype='OPM';";
	//echo "<br>".$sql;
	$rs=mysql_query($sql);
	if(($last_rev_status=="สำเร็จ") && ($rs)){
		echo "<br>ดึงข้อมูลจาก OPM !!!สำเร็จ!!!";
	}else{
		echo "<br>ดึงข้อมูลจาก OPM !!!ล้มเหลว!!!";
	}
	mysql_close();
?> 
